let startY, moveY;
Component({
  properties: {
    tipStayTime:{
      type: Number,
      value: 2000
    },
    isTip: {
      type: Boolean,
      value: true
    },
    tipText: {
      type: String,
      value: '已刷新成功'
    },
    iconColor:{
      type: String,
      value: '#2F9FB2'
    },
    noMore:{
      type: Boolean,
      value: false
    }
  },
  data: {
    y: 0,
    isMask: true,
    isShowTip: false,
    refreshing: false,
  },
  methods: {
    scroll(e) {
      const { scrollTop } = e.detail;
      scrollTop == 0 ? this.setData({
        isMask: true
      }) : this.setData({
        isMask: false
      })
    },
    tolower() {
      !this.data.noMore && this.triggerEvent("load");
    },
    touchstart(e) {
      console.log(this.data.refreshing)
      if (this.data.refreshing) return false;
      startY = e.touches[0].pageY;
    },
    touchmove(e) {
      if (this.data.refreshing) return false;
      const { pageY } = e.touches[0];
      moveY = pageY - startY;
      if (moveY > 0 && moveY <= 180) {
        this.setData({ y: moveY });
      } else if (moveY < 0) {
        this.setData({
          isMask: false
        })
      };
    },
    touchend(e) {
      if (this.data.refreshing) return false;
      if (moveY > 80) {
        this.setData({ refreshing: true, y: 60 })
        this.triggerEvent("refresh");
      } else {
        this.setData({ y: 0 })
      }
      moveY = 0, startY = 0;
    },
    refreshSuccess(){
      const _this = this;
      _this.setData({ y: 0 }, function () {
        _this.setData({
          refreshing: false,
        })
      })
      setTimeout(function () {
        _this.setData({
          isShowTip: true
        }, function () {
          setTimeout(function () {
            _this.setData({
              isShowTip: false
            })
          }, _this.data.tipStayTime)
        })
      }, 300)
    }
  }
})
